package com.ruoyi.business.service.impl;

import java.util.*;

import com.ruoyi.business.domain.WmsInWarehous;
import com.ruoyi.common.utils.BarCodeUtils;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.WmsDeliveryRecordMapper;
import com.ruoyi.business.domain.WmsDeliveryRecord;
import com.ruoyi.business.service.IWmsDeliveryRecordService;

/**
 * 出库单Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-13
 */
@Service
public class WmsDeliveryRecordServiceImpl implements IWmsDeliveryRecordService
{
    @Autowired
    private WmsDeliveryRecordMapper wmsDeliveryRecordMapper;

    /**
     * 查询出库单
     *
     * @param id 出库单主键
     * @return 出库单
     */
    @Override
    public WmsDeliveryRecord selectWmsDeliveryRecordById(Long id)
    {
        WmsDeliveryRecord wmsDeliveryRecord = wmsDeliveryRecordMapper.selectWmsDeliveryRecordById(id);
        if (wmsDeliveryRecord != null) {
            wmsDeliveryRecord.setBarCode(BarCodeUtils.barCodeImageBase64(wmsDeliveryRecord.getDeliveryNo()));
        }
        return wmsDeliveryRecord;
    }

    @Override
    public WmsDeliveryRecord selectWmsDeliveryRecordByDeliveryNo(String deliveryNo) {
        return wmsDeliveryRecordMapper.selectWmsDeliveryRecordByDeliveryNo(deliveryNo);
    }

    /**
     * 查询出库单列表
     *
     * @param wmsDeliveryRecord 出库单
     * @return 出库单
     */
    @Override
    public List<WmsDeliveryRecord> selectWmsDeliveryRecordList(WmsDeliveryRecord wmsDeliveryRecord)
    {
        return wmsDeliveryRecordMapper.selectWmsDeliveryRecordList(wmsDeliveryRecord);
    }

    /**
     * 新增出库单
     *
     * @param wmsDeliveryRecord 出库单
     * @return 结果
     */
    @Override
    public int insertWmsDeliveryRecord(WmsDeliveryRecord wmsDeliveryRecord)
    {
        wmsDeliveryRecord.setCreateTime(DateUtils.getNowDate());
        return wmsDeliveryRecordMapper.insertWmsDeliveryRecord(wmsDeliveryRecord);
    }

    /**
     * 修改出库单
     *
     * @param wmsDeliveryRecord 出库单
     * @return 结果
     */
    @Override
    public int updateWmsDeliveryRecord(WmsDeliveryRecord wmsDeliveryRecord)
    {
        wmsDeliveryRecord.setUpdateTime(DateUtils.getNowDate());
        return wmsDeliveryRecordMapper.updateWmsDeliveryRecord(wmsDeliveryRecord);
    }

    /**
     * 批量删除出库单
     *
     * @param ids 需要删除的出库单主键
     * @return 结果
     */
    @Override
    public int deleteWmsDeliveryRecordByIds(Long[] ids)
    {
        return wmsDeliveryRecordMapper.deleteWmsDeliveryRecordByIds(ids);
    }

    /**
     * 删除出库单信息
     *
     * @param id 出库单主键
     * @return 结果
     */
    @Override
    public int deleteWmsDeliveryRecordById(Long id)
    {
        return wmsDeliveryRecordMapper.deleteWmsDeliveryRecordById(id);
    }

    /**
     * 出入库统计
     * @param info
     * @return
     */
    @Override
    public List<Map<String, Object>> statInOut(WmsInWarehous info) {
        List<Map<String, Object>> list = null;
        if(info.getStatType() == 1){
            //近七天
            Map<String,Object> params = new HashMap<>();
            params.put("endTime",DateUtils.parseDateToStr("yyyy-MM-dd",DateUtils.addDays(new Date(),-6)));
            list = wmsDeliveryRecordMapper.statNumBy7Day(params);
            List<Map<String,Object>> dataList = getMonthDateList(DateUtils.addDays(new Date(),-6),new Date());
            for(Map<String,Object> map1 : dataList){
                for(Map<String,Object> map2 : list){
                    if(map1.get("date").equals(map2.get("date"))){
                        map1.put("num",map2.get("num"));
                    }
                }
            }
            list = dataList;
        }else if(info.getStatType() == 2){
            //本月
            Map<String,Object> params = new HashMap<>();
            params.put("endTime",DateUtils.parseDateToStr("yyyy-MM",new Date()));
            list = wmsDeliveryRecordMapper.statNumByNowMonth(params);
            String time1 = DateUtils.parseDateToStr("yyyy-MM-dd",DateUtils.getMonthFirstDay());
            String time2 = DateUtils.parseDateToStr("yyyy-MM-dd",DateUtils.getMonthLastDay());
            List<Map<String,Object>> dataList = getMonthDateList(DateUtils.dateTime("yyyy-MM-dd",time1),DateUtils.dateTime("yyyy-MM-dd",time2));
            for(Map<String,Object> map1 : dataList){
                for(Map<String,Object> map2 : list){
                    if(map1.get("date").equals(map2.get("date"))){
                        map1.put("num",map2.get("num"));
                    }
                }
            }
            list = dataList;
        }else if(info.getStatType() == 3 || info.getStatType() == 4){
            //本年 或者自定义
            Map<String,Object> params = new HashMap<>();
            params.put("endTime",DateUtils.parseDateToStr("yyyy",new Date()));
            list = wmsDeliveryRecordMapper.statNumByNowYear(params);

            List<Map<String,Object>> dataList = getYearDateList(DateUtils.parseDateToStr("yyyy",new Date()));
            for(Map<String,Object> map1 : dataList){
                for(Map<String,Object> map2 : list){
                    if(map1.get("date").equals(map2.get("date"))){
                        map1.put("num",map2.get("num"));
                    }
                }
            }
            list = dataList;
        }
        return list;
    }

    public List<Map<String,Object>> getMonthDateList(Date startTime,Date endTime){
        String time2 = DateUtils.parseDateToStr("yyyy-MM-dd",endTime);
        List<Map<String,Object>> dateList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("date",DateUtils.parseDateToStr("yyyy-MM-dd",startTime));
        map.put("num",0);
        dateList.add(map);
        for(int i=1;i<=3650;i++){
            Date nowDate = DateUtils.addDays(startTime,i);
            String time1 = DateUtils.parseDateToStr("yyyy-MM-dd",nowDate);
            if(nowDate.getTime() <= endTime.getTime()){
                Map<String,Object> m = new HashMap<>();
                m.put("date",time1);
                m.put("num",0);
                dateList.add(m);
            }else{
                break;
            }
        }
        return dateList;
    }

    public List<Map<String,Object>> getYearDateList(String year){
        List<Map<String,Object>> dateList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("date",year+"-01");
        map.put("num",0);
        dateList.add(map);
        for(int i=2;i<=12;i++){
            if(i<10){
                Map<String,Object> map1 = new HashMap<>();
                map1.put("date",year+"-0"+i);
                map1.put("num",0);
                dateList.add(map1);
            }else{
                Map<String,Object> map1 = new HashMap<>();
                map1.put("date",year+"-"+i);
                map1.put("num",0);
                dateList.add(map1);
            }
        }
        return dateList;
    }
}
